<template>
    <div class="orderAddressSelect list-wrap-pk">
        <div v-if="hasAddress" @click="toUserAddressList" class="d-flex-cpb-pk defaultAddress list-item-pk item-content-pk">
            <van-icon name="location" color="#D82D11" size="20px" />
            <div>
                <div>{{expressInfo.name}} <span class="text-light-pk">{{expressInfo.mobile}}</span></div>
                <div class="break-all-pk">{{addressStr}}</div>
            </div>
            <van-icon name="arrow" size="18px" class="text-light-pk" />
        </div>
        <pk-cell v-else
                 pad-h="10px"
                 title="手动添加收货地址"
                 icon="add"
                 isLink @click="addAddress">
        </pk-cell>
        <address-pop ref="addressAdd" @change="addSuccess"></address-pop>
    </div>
</template>

<script>
    import PkCell from "./PkCell";
    import AddressPop from "./AddressPop";
    export default {
        name: "orderAddressSelect",
        components: {AddressPop, PkCell},

        data() {
            return {
                hasAddress: false,
                expressInfo: {}
            }
        },

        created() {
            this.getUserExpress()
        },
        computed: {
            addressStr() {
                return `${this.expressInfo.province} ${this.expressInfo.city} ${this.expressInfo.area} ${this.expressInfo.address}`
            },
        },
        methods: {
            addAddress() {
                this.$refs.addressAdd.addAddress()
            },
            addSuccess(val) {
                this.expressInfo = val;
                this.hasAddress = true;
                this.$emit("change",this.expressInfo.expressId)
            },
            getUserExpress() {
                this.$api.getUserExpress().then(res => {
                    let result = this.$resData(res);
                    if(result.length > 0) {
                        this.hasAddress = true;
                        let tempArr = [];
                        tempArr = result.filter( item => {
                            return item.isDefault === '1'
                        });
                        if(tempArr.length > 0) {
                            this.expressInfo = tempArr[0]
                        }else {
                            this.expressInfo = result[0]
                        }
                        this.$emit("change",this.expressInfo.expressId)
                    }
                })
            },
            toUserAddressList() {
                this.$router.push({path: "/addressList"})
            }
        }
    }
</script>

<style lang="scss" scoped>
    .orderAddressSelect {

    }
</style>
